package com.yzm;

import java.util.Arrays;

public class SortDemo {
    public static void shellSort(int[] arr) {
        int temp, count = 1;
        for (int gap = arr.length / 2; gap > 0; gap /= 2) {
            //变化gap的值
            for (int i = gap; i < arr.length; i++) {
                //分组,遍历所有元素
                for (int j = i - gap; j >= 0; j -= gap) {
                    if (arr[j] > arr[j + gap])  {
                        temp = arr[j + gap];
                        arr[j + gap] = arr[j];
                        arr[j] = temp;
                    }
                }
            }
        }
        System.out.println("希尔排序后:" + Arrays.toString(arr));
    }

    public static void insertSort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            int insertValue = arr[i];
            int insertIndex = i - 1;
            while (insertIndex >= 0 && arr[insertIndex] > insertValue) {
                arr[insertIndex + 1] = arr[insertIndex];
                insertIndex--;
            }
            arr[insertIndex + 1] = insertValue;
        }
        System.out.println("插入排序后:" + Arrays.toString(arr));
    }

    public static void bubbleSort(int[] arr) {
        int temp;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    temp = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = temp;
                }
            }
        }
        System.out.println("冒泡排序后:" + Arrays.toString(arr));
    }

    public static void choiceSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int min = arr[i];
            int minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (min > arr[j]) {
                    min = arr[j];
                    minIndex = j;
                }
            }
            arr[minIndex] = arr[i];
            arr[i] = min;
        }
        System.out.println("选择排序后:" + Arrays.toString(arr));
    }

    public static void quickSort(int[] arr, int left, int right) {
        int l = left;
        int r = right;
        int temp = 0;
        int pivot = arr[(left + right) / 2];
        while (l < r) {
            while (l < r && arr[l] < pivot) {
                l++;
            }
            while (l < r && arr[r] > pivot) {
                r--;
            }
            if (l >= r) {
                break;
            }
            temp = arr[l];
            arr[l] = arr[r];
            arr[r] = temp;
            //如果交换完后发现，arr[l] == pivot ,r--,前移
            if (arr[l] == pivot) {
                r -= 1;
            }
            //如果交换完后发现，arr[r] == pivot ,l++-,后移
            if (arr[r] == pivot) {
                l += 1;
            }
        }
        if (l == r) {
            l += 1;
            r -= 1;
        }
        //向左递归
        if (left < r) {
            quickSort(arr, left, r);
        }
        //向右递归
        if (right > l) {
            quickSort(arr, l, right);
        }
    }

    public static void heapSort(int[] arr) {
        int temp;
        for (int i = arr.length / 2 - 1; i >= 0; i--) {
            adjustHeap(arr, i, arr.length);
        }
        for (int j = arr.length - 1; j > 0; j--) { //调整次数
            temp = arr[j];
            arr[j] = arr[0];
            arr[0] = temp;
            adjustHeap(arr, 0, j);
        }
        System.out.println("堆排序后:" + Arrays.toString(arr));
    }

    //将一个数组调整成一个大顶堆

    /**
     * 完成以i为对应的非叶子结点的树构建成大顶堆
     *
     * @param arr    待调整的数组
     * @param i      最后一个非叶子结点在数组中的索引
     * @param length 对多少个元素进行调整，length逐渐减少
     */
    public static void adjustHeap(int[] arr, int i, int length) {
        int temp = arr[i];
        //k-->以i为叶子结点的左子节点
        for (int k = i * 2 + 1; k < length; k = k * 2 + 1) {
            if (k + 1 < length && arr[k] < arr[k + 1]) { //左子节点的值小于右子节点的值
                k++;
            }
            if (arr[k] > temp) {
                //如果最大值大于父节点的值
                arr[i] = arr[k]; //把较大值赋给当前节点
                i = k; //i指向k继续循环比较
            } else {
                break;
            }
        }
        //for循环结束后，将以i为父节点的树的最大值放到了顶部
        arr[i] = temp; //将temp放到调整后的位置
    }

    public static void mergeSort(int[] arr, int left, int right, int[] temp) {
        if (left < right) {
            int mid = (left + right) / 2; //中间索引
            //向左递归
            mergeSort(arr, left, mid, temp);
            //向右递归
            mergeSort(arr, mid + 1, right, temp);
            merge(arr, left, mid, right, temp);
        }
    }

    /**
     * @param arr   待排序的数组
     * @param left  左边有序序列的初始索引
     * @param mid   中间的索引
     * @param right 右边索引
     * @param temp  中转数组
     */
    public static void merge(int[] arr, int left, int mid, int right, int[] temp) {
        int i = left;
        int j = mid + 1; //右边有序序列的初始索引
        int t = 0; //指向temp数组的索引
        while (i <= mid && j <= right) {
            if (arr[i] <= arr[j]) {
                temp[t++] = arr[i++];
            } else {
                temp[t++] = arr[j++];
            }
        }
        while (i <= mid) { //左边的有序序列还有剩余
            temp[t++] = arr[i++];
        }
        while (j <= right) {
            temp[t++] = arr[j++];
        }
        t = 0;
        int tempLeft = left;
        while (tempLeft <= right) {
            arr[tempLeft++] = temp[t++];
        }
    }
}
